package com.tql.mall.boss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tql.mall.boss.mapper.BaseCategoryViewMapper;
import com.tql.mall.boss.service.BaseCategoryViewService;
import com.tql.mall.model.product.BaseCategoryView;
import com.tql.mall.model.product.level.BaseLevel;
import com.tql.mall.model.product.level.FirstLevel;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BaseCategoryViewServiceImpl extends ServiceImpl<BaseCategoryViewMapper, BaseCategoryView> implements BaseCategoryViewService {

    /**
     * 根据三级分类的 id，获取BaseCategoryView对象
     * @return
     */
    @Override
    public BaseCategoryView getCategoryView(Long id) {
        QueryWrapper<BaseCategoryView> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id",id);
        BaseCategoryView baseCategoryView = baseMapper.selectOne(queryWrapper);
        return baseCategoryView;
    }

    @Override
    public BaseCategoryView getCategoryViewById2(Long id) {
        QueryWrapper<BaseCategoryView> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category2_id",id);
        BaseCategoryView baseCategoryView = baseMapper.selectList(queryWrapper).get(0);
        baseCategoryView.setCategory3Id(null);
        baseCategoryView.setCategory3Name(null);
        return baseCategoryView;
    }

    @Override
    public BaseCategoryView getCategoryViewById1(Long id) {
        QueryWrapper<BaseCategoryView> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category1_id",id);
        BaseCategoryView baseCategoryView = baseMapper.selectList(queryWrapper).get(0);
        baseCategoryView.setCategory2Id(null);
        baseCategoryView.setCategory2Name(null);
        baseCategoryView.setCategory3Id(null);
        baseCategoryView.setCategory3Name(null);
        return baseCategoryView;
    }

    /**
     * 以嵌套的形式返回三级分类
     *
     * @return
     */
    @Override
    public List<FirstLevel> getAllClassifyInfo() {
        List<BaseCategoryView> list = baseMapper.selectList(new QueryWrapper<>());
        //封装成嵌套的结构：index从1开始
        int index = 0;
        ArrayList<FirstLevel> res = new ArrayList<>();
        Long id1 = -1L;
        Long id2 = -1L;
        FirstLevel cur = null;
        BaseLevel cur2 = null;
        for (BaseCategoryView bcv:list){
            if (!bcv.getCategory1Id().equals(id1)){
                FirstLevel firstLevel = new FirstLevel();
                firstLevel.setIndex(++index);
                firstLevel.setCategoryId(bcv.getCategory1Id());
                firstLevel.setCategoryName(bcv.getCategory1Name());
                id1 = bcv.getCategory1Id();
                res.add(firstLevel);
                cur = firstLevel;
            }
            if (!bcv.getCategory2Id().equals(id2)){
                if (cur.getCategoryChild() == null){
                    cur.setCategoryChild(new ArrayList<>());
                }
                BaseLevel baseLevel2 = new BaseLevel();
                baseLevel2.setCategoryId(bcv.getCategory2Id());
                baseLevel2.setCategoryName(bcv.getCategory2Name());
                cur.getCategoryChild().add(baseLevel2);
                id2 = bcv.getCategory2Id();
                cur2 = baseLevel2;
            }
            BaseLevel baseLevel3 = new BaseLevel();
            baseLevel3.setCategoryName(bcv.getCategory3Name());
            baseLevel3.setCategoryId(bcv.getCategory3Id());
            if (cur2.getCategoryChild()==null){
                cur2.setCategoryChild(new ArrayList<>());
            }
            cur2.getCategoryChild().add(baseLevel3);
        }

        return res;
    }
}
